package com.example.elmmerchants.Order_Setting;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Set_Open_Shop_Time extends AppCompatActivity {

    RelativeLayout choose_which_day_open_in_week, add_3;
    AlertDialog dialogs;
    TextView which_day_open, start_time_hour, start_time_min, end_time_hour, end_time_min, start_time_hour_2, start_time_min_2, end_time_hour_2, end_time_min_2;
    LinearLayout choose_start_time, choose_end_time, add_open_time, add_time_2, choose_start_time_2, choose_end_time_2;
    Button finish;
    ImageButton delete_add_time;

    String open_day, open_day_text = "", email, open_time, close_time, open_hour = "", open_min = "", close_hour = "", close_min = "";
    boolean is_mon = false, is_tue = false, is_wed = false, is_thu = false, is_fri = false, is_sat = false, is_sun = false, is_set_time_2 = false;
    Map<Character, String> week = new HashMap<>();
    String open_hour_2 = "";
    String close_hour_2 = "";

    String[] all_hour = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
    String[] all_min = new String[]{"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_open_shop_time);

        week.put('1', "周一");
        week.put('2', "周二");
        week.put('3', "周三");
        week.put('4', "周四");
        week.put('5', "周五");
        week.put('6', "周六");
        week.put('7', "周日");

        open_day = getIntent().getStringExtra("open_day");
        email = getIntent().getStringExtra("email");
        open_time = getIntent().getStringExtra("open_time");
        close_time = getIntent().getStringExtra("close_time");

        open_hour += open_time.charAt(0);
        open_hour += open_time.charAt(1);
        open_min += open_time.charAt(3);
        open_min += open_time.charAt(4);

        close_hour += close_time.charAt(0);
        close_hour += close_time.charAt(1);
        close_min += close_time.charAt(3);
        close_min += close_time.charAt(4);

        choose_which_day_open_in_week = findViewById(R.id.choose_which_day_open_in_week);
        which_day_open = findViewById(R.id.which_day_open);
        choose_start_time = findViewById(R.id.choose_start_time);
        choose_end_time = findViewById(R.id.choose_end_time);
        start_time_hour = findViewById(R.id.start_time_hour);
        start_time_min = findViewById(R.id.start_time_min);
        end_time_hour = findViewById(R.id.end_time_hour);
        end_time_min = findViewById(R.id.end_time_min);
        finish = findViewById(R.id.finish);
        add_open_time = findViewById(R.id.add_open_time);
        add_time_2 = findViewById(R.id.add_time_2);
        delete_add_time = findViewById(R.id.delete_add_time);
        start_time_hour_2 = findViewById(R.id.start_time_hour_2);
        start_time_min_2 = findViewById(R.id.start_time_min_2);
        choose_start_time_2 = findViewById(R.id.choose_start_time_2);
        choose_end_time_2 = findViewById(R.id.choose_end_time_2);
        end_time_hour_2 = findViewById(R.id.end_time_hour_2);
        end_time_min_2 = findViewById(R.id.end_time_min_2);

        start_time_hour.setText(open_hour);
        start_time_min.setText(open_min);
        end_time_hour.setText(close_hour);
        end_time_min.setText(close_min);

        set_open_day_text();

        choose_which_day_open_in_week.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.choose_which_week, null);
                CheckBox mon = view1.findViewById(R.id.mon);
                mon.setChecked(is_mon);
                CheckBox tue = view1.findViewById(R.id.tue);
                tue.setChecked(is_tue);
                CheckBox wed = view1.findViewById(R.id.wed);
                wed.setChecked(is_wed);
                CheckBox thu = view1.findViewById(R.id.thu);
                thu.setChecked(is_thu);
                CheckBox fri = view1.findViewById(R.id.fri);
                fri.setChecked(is_fri);
                CheckBox sat = view1.findViewById(R.id.sat);
                sat.setChecked(is_sat);
                CheckBox sun = view1.findViewById(R.id.sun);
                sun.setChecked(is_sun);
                builder.setView(view1);
                builder.setCancelable(false);
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        open_day = "";
                        if (mon.isChecked()) {
                            open_day += "1,";
                        }
                        if (tue.isChecked()) {
                            open_day += "2,";
                        }
                        if (wed.isChecked()) {
                            open_day += "3,";
                        }
                        if (thu.isChecked()) {
                            open_day += "4,";
                        }
                        if (fri.isChecked()) {
                            open_day += "5,";
                        }
                        if (sat.isChecked()) {
                            open_day += "6,";
                        }
                        if (sun.isChecked()) {
                            open_day += "7,";
                        }
                        set_open_day_text();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                dialogs = builder.create();
                dialogs.show();
            }
        });

        choose_start_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start_time_hour.setText(open_hour);
                start_time_min.setText(open_min);
                end_time_hour.setText(close_hour);
                end_time_min.setText(close_min);
                start_time_hour_2.setText("");
                start_time_min_2.setText("");
                end_time_hour_2.setText("");
                end_time_min_2.setText("");
                Dialog dialog = new Dialog(view.getContext());
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.choose_start_time_bottomsheet);
                NumberPicker hour = dialog.findViewById(R.id.hour);
                hour.setDisplayedValues(all_hour);
                hour.setMaxValue(all_hour.length - 1);
                hour.setMinValue(0);
                hour.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                hour.setValue(Integer.parseInt(open_hour));

                NumberPicker min = dialog.findViewById(R.id.min);
                min.setDisplayedValues(all_min);
                min.setMaxValue(all_min.length - 1);
                min.setMinValue(0);
                min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                min.setValue(Integer.parseInt(open_min));
                TextView finish = dialog.findViewById(R.id.finish);
                finish.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        start_time_hour.setText(all_hour[hour.getValue()]);
                        start_time_min.setText(all_min[min.getValue()]);
                        dialog.dismiss();
                    }
                });
                TextView cancel = dialog.findViewById(R.id.cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                dialog.getWindow().setGravity(Gravity.BOTTOM);
            }
        });

        choose_end_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start_time_hour_2.setText("");
                start_time_min_2.setText("");
                end_time_hour_2.setText("");
                end_time_min_2.setText("");
                String[] s;
                String[] s_min;
                int s_length = 0;
                if (Integer.parseInt(start_time_min.getText().toString()) < 59) {
                    s = new String[24 - Integer.parseInt(start_time_hour.getText().toString())];
                    s[0] = start_time_hour.getText().toString();
                    s_length = 1;
                    s_min = new String[59 - Integer.parseInt(start_time_min.getText().toString())];
                    int s_min_length = 0;
                    for (int i = Integer.parseInt(start_time_min.getText().toString()) + 1; i < 60; i++) {
                        if (String.valueOf(i).length() == 1) {
                            s_min[s_min_length] = "0" + i;
                        }
                        else {
                            s_min[s_min_length] = String.valueOf(i);
                        }
                        s_min_length += 1;
                    }
                }
                else {
                    s = new String[24 - Integer.parseInt(start_time_hour.getText().toString()) - 1];
                    s_min = new String[0];
                }
                for (int i = Integer.parseInt(start_time_hour.getText().toString()) + 1; i < 24; i++) {
                    if (String.valueOf(i).length() == 1) {
                        s[s_length] = "0" + i;
                    }
                    else {
                        s[s_length] = String.valueOf(i);
                    }
                    s_length += 1;
                }

                if (s.length > 1 || s_min.length > 1) {
                    Dialog dialog = new Dialog(view.getContext());
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.choose_end_time_bottomsheet);
                    NumberPicker min = dialog.findViewById(R.id.min);
                    NumberPicker hour = dialog.findViewById(R.id.hour);
                    hour.setDisplayedValues(s);
                    hour.setMaxValue(s.length - 1);
                    hour.setMinValue(0);
                    hour.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                    hour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                        @Override
                        public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                            System.out.println(numberPicker.getValue());

                            if (numberPicker.getValue() == 0 && s_min.length > 0) {
                                min.setMinValue(0);
                                min.setMaxValue(s_min.length - 1);
                                min.setDisplayedValues(s_min);
                                min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                min.setValue(0);
                            }
                            else {
                                min.setDisplayedValues(all_min);
                                min.setMaxValue(all_min.length - 1);
                                min.setMinValue(0);
                                min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                min.setValue(0);
                            }
                        }
                    });

                    if (hour.getValue() == 0 && s_min.length > 0) {
                        min.setMinValue(0);
                        min.setMaxValue(s_min.length - 1);
                        min.setDisplayedValues(s_min);
                        min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                        min.setValue(0);
                    }
                    else {
                        min.setDisplayedValues(all_min);
                        min.setMaxValue(all_min.length - 1);
                        min.setMinValue(0);
                        min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                        min.setValue(0);
                    }
                    TextView finish = dialog.findViewById(R.id.finish);
                    finish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            end_time_hour.setText(s[hour.getValue()]);
                            if (hour.getValue() == 0) {
                                end_time_min.setText(s_min[min.getValue()]);
                            }
                            else {
                                end_time_min.setText(all_min[min.getValue()]);
                            }
                            dialog.dismiss();
                        }
                    });
                    TextView cancel = dialog.findViewById(R.id.cancel);
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                    dialog.getWindow().setGravity(Gravity.BOTTOM);
                }
                else {
                    Toast.makeText(Set_Open_Shop_Time.this, "请减少起始时间", Toast.LENGTH_SHORT).show();
                }
            }
        });

        finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ((!start_time_hour_2.getText().toString().isEmpty() && !end_time_hour_2.getText().toString().isEmpty()) || start_time_hour_2.getText().toString().isEmpty()) {
                    if (!start_time_hour_2.getText().toString().isEmpty()) {
                        open_hour_2 = start_time_hour_2.getText().toString() + ":" + start_time_min_2.getText().toString();
                        close_hour_2 = end_time_hour_2.getText().toString() + ":" + end_time_min_2.getText().toString();
                    }
                    ExecutorService executorService = Executors.newSingleThreadExecutor();
                    executorService.execute(() -> {
                        try {
                            Connection_SQL userSQL = new Connection_SQL();
                            Connection connection = userSQL.connection();
                            String sql2 = "update `merchant` set open_day = ?, open_hour = ?, close_hour = ?, open_hour2 = ?, close_hour2 = ? where email = ?";
                            PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);
                            preparedStatement2.setString(1, open_day);
                            preparedStatement2.setString(2, start_time_hour.getText().toString() + ":" + start_time_min.getText().toString());
                            preparedStatement2.setString(3, end_time_hour.getText().toString() + ":" + end_time_min.getText().toString());
                            preparedStatement2.setString(4, open_hour_2);
                            preparedStatement2.setString(5, close_hour_2);
                            preparedStatement2.setString(6, email);
                            preparedStatement2.executeUpdate();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        runOnUiThread(() -> {
                            try {
                                Thread.sleep(100);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            Intent intent = new Intent(Set_Open_Shop_Time.this, Book_Order_Setting.class);
                            intent.putExtra("open_time", start_time_hour.getText().toString() + ":" + start_time_min.getText().toString());
                            intent.putExtra("end_time", end_time_hour.getText().toString() + ":" + end_time_min.getText().toString());
                            intent.putExtra("open_time_2", open_hour_2);
                            intent.putExtra("end_time_2", close_hour_2);
                            setResult(1, intent);
                            finish();
                        });
                    });
                }
                else {
                    Toast.makeText(Set_Open_Shop_Time.this, "请输入完整信息", Toast.LENGTH_SHORT).show();
                }
            }
        });

        add_open_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add_open_time.setVisibility(View.GONE);
                add_time_2.setVisibility(View.VISIBLE);
                delete_add_time.setVisibility(View.VISIBLE);
                is_set_time_2 = true;
            }
        });

        delete_add_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                start_time_hour_2.setText("");
                start_time_min_2.setText("");
                end_time_min_2.setText("");
                end_time_hour_2.setText("");
                add_open_time.setVisibility(View.VISIBLE);
                add_time_2.setVisibility(View.GONE);
                delete_add_time.setVisibility(View.GONE);
                is_set_time_2 = false;
            }
        });

        choose_start_time_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                end_time_hour_2.setText("");
                end_time_min_2.setText("");
                String[] s;
                String[] s_min;
                String[] last_min;
                int s_length = 0;
                if (Integer.parseInt(end_time_min.getText().toString()) < 59) {
                    if (Integer.parseInt(start_time_min.getText().toString()) > 0) {
                        s = new String[24 - Integer.parseInt(end_time_hour.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString()) + 1];
                    }
                    else {
                        s = new String[24 - Integer.parseInt(end_time_hour.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString())];
                    }
                    s[0] = end_time_hour.getText().toString();
                    s_length = 1;
                    s_min = new String[59 - Integer.parseInt(end_time_min.getText().toString())];
                    int s_min_length = 0;
                    for (int i = Integer.parseInt(end_time_min.getText().toString()) + 1; i < 60; i++) {
                        if (String.valueOf(i).length() == 1) {
                            s_min[s_min_length] = "0" + i;
                        }
                        else {
                            s_min[s_min_length] = String.valueOf(i);
                        }
                        s_min_length += 1;
                    }
                }
                else {
                    if (Integer.parseInt(start_time_min.getText().toString()) > 0) {
                        s = new String[24 - Integer.parseInt(end_time_hour.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString())];
                    }
                    else {
                        s = new String[24 - Integer.parseInt(end_time_hour.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString()) - 1];
                    }
                    s_min = new String[0];
                }
                for (int i = Integer.parseInt(end_time_hour.getText().toString()) + 1; i < 24; i++) {
                    if (i < 10) {
                        s[s_length] = "0" + i;
                    }
                    else {
                        s[s_length] = String.valueOf(i);
                    }
                    s_length += 1;
                }

                for (int i = 0; i < Integer.parseInt(start_time_hour.getText().toString()); i++) {
                    if (i < 10) {
                        s[s_length] = "0" + i;
                    }
                    else {
                        s[s_length] = String.valueOf(i);
                    }
                    s_length += 1;
                }

                if (Integer.parseInt(start_time_min.getText().toString()) > 0) {
                    s[s_length] = start_time_hour.getText().toString();
                    last_min = new String[Integer.parseInt(start_time_min.getText().toString())];
                    int last_min_length = 0;
                    for (int i = 0; i < Integer.parseInt(start_time_min.getText().toString()); i++) {
                        if (i < 10) {
                            last_min[last_min_length] = "0" + i;
                        }
                        else {
                            last_min[last_min_length] = String.valueOf(i);
                        }
                        last_min_length += 1;
                    }
                }
                else {
                    last_min = new String[0];
                }

                if (s.length > 1 || s_min.length > 1) {
                    Dialog dialog = new Dialog(view.getContext());
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.choose_start_time_bottomsheet);
                    NumberPicker min = dialog.findViewById(R.id.min);
                    NumberPicker hour = dialog.findViewById(R.id.hour);
                    hour.setDisplayedValues(s);
                    hour.setMaxValue(s.length - 1);
                    hour.setMinValue(0);
                    hour.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                    hour.setValue(0);
                    hour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                        @Override
                        public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                            System.out.println(numberPicker.getValue());
                            System.out.println(s.length);

                            if (numberPicker.getValue() == 0 && s_min.length > 0) {
                                min.setDisplayedValues(null);
                                min.setMaxValue(s_min.length - 1);
                                min.setDisplayedValues(s_min);
                                min.setMinValue(0);
                                min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                min.setValue(0);
                            }
                            else if (numberPicker.getValue() == s.length - 1 && last_min.length > 0) {
                                min.setDisplayedValues(null);
                                min.setMaxValue(last_min.length - 1);
                                min.setDisplayedValues(last_min);
                                min.setMinValue(0);
                                min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                min.setValue(0);
                            }
                            else {
                                min.setDisplayedValues(null);
                                min.setMaxValue(all_min.length - 1);
                                min.setDisplayedValues(all_min);
                                min.setMinValue(0);
                                min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                min.setValue(0);
                            }
                        }
                    });

                    if (hour.getValue() == 0 && s_min.length > 0) {
                        min.setMaxValue(s_min.length - 1);
                        min.setDisplayedValues(s_min);
                        min.setMinValue(0);
                        min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                        min.setValue(0);
                    }
                    else if (hour.getValue() == s.length - 1 && last_min.length > 0) {
                        min.setMaxValue(last_min.length - 1);
                        min.setDisplayedValues(last_min);
                        min.setMinValue(0);
                        min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                        min.setValue(0);
                    }
                    else {
                        min.setMaxValue(all_min.length - 1);
                        min.setDisplayedValues(all_min);
                        min.setMinValue(0);
                        min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                        min.setValue(0);
                    }
                    TextView finish = dialog.findViewById(R.id.finish);
                    finish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            start_time_hour_2.setText(s[hour.getValue()]);
                            if (hour.getValue() == 0 && s_min.length > 0) {
                                start_time_min_2.setText(s_min[min.getValue()]);
                            }
                            else if (hour.getValue() == s.length - 1 && last_min.length > 0) {
                                start_time_min_2.setText(last_min[min.getValue()]);
                            }
                            else {
                                start_time_min_2.setText(all_min[min.getValue()]);
                            }
                            dialog.dismiss();
                        }
                    });
                    TextView cancel = dialog.findViewById(R.id.cancel);
                    cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                    dialog.getWindow().setGravity(Gravity.BOTTOM);
                }
                else {
                    Toast.makeText(Set_Open_Shop_Time.this, "没有可选择的时间", Toast.LENGTH_SHORT).show();
                }
            }
        });

        choose_end_time_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!start_time_hour_2.getText().toString().isEmpty()) {
                    String[] s;
                    String[] s_min;
                    String[] last_min;
                    int s_length = 0;
                    if (Integer.parseInt(start_time_min_2.getText().toString()) < 59) {
                        if (Integer.parseInt(start_time_min.getText().toString()) > 0) {
                            if (Integer.parseInt(start_time_hour.getText().toString()) < Integer.parseInt(start_time_hour_2.getText().toString())) {
                                s = new String[24 - Integer.parseInt(start_time_hour_2.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString()) + 1];
                            }
                            else if (Integer.parseInt(start_time_hour.getText().toString()) == Integer.parseInt(start_time_hour_2.getText().toString()) && Integer.parseInt(start_time_min.getText().toString()) < Integer.parseInt(start_time_min_2.getText().toString())) {
                                s = new String[1];
                            }
                            else {
                                s = new String[Integer.parseInt(start_time_hour.getText().toString()) - Integer.parseInt(start_time_hour_2.getText().toString()) + 1];
                             }
                        }
                        else {
                            if (Integer.parseInt(start_time_hour.getText().toString()) < Integer.parseInt(start_time_hour_2.getText().toString())) {
                                s = new String[24 - Integer.parseInt(start_time_hour_2.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString())];
                            }
                            else if (Integer.parseInt(start_time_hour.getText().toString()) == Integer.parseInt(start_time_hour_2.getText().toString()) && Integer.parseInt(start_time_min.getText().toString()) < Integer.parseInt(start_time_min_2.getText().toString())) {
                                s = new String[1];
                            }
                            else {
                                s = new String[Integer.parseInt(start_time_hour.getText().toString()) - Integer.parseInt(start_time_hour_2.getText().toString())];
                            }
                        }
                        s[0] = start_time_hour_2.getText().toString();
                        s_length = 1;
                        if (Integer.parseInt(start_time_hour.getText().toString()) == Integer.parseInt(start_time_hour_2.getText().toString()) && Integer.parseInt(start_time_min_2.getText().toString()) < Integer.parseInt(start_time_min.getText().toString())) {
                            s_min = new String[Integer.parseInt(start_time_min.getText().toString()) - Integer.parseInt(start_time_min_2.getText().toString()) - 1];
                            int s_min_length = 0;
                            for (int i = Integer.parseInt(start_time_min_2.getText().toString()) + 1; i < Integer.parseInt(start_time_min.getText().toString()); i++) {
                                if (String.valueOf(i).length() == 1) {
                                    s_min[s_min_length] = "0" + i;
                                }
                                else {
                                    s_min[s_min_length] = String.valueOf(i);
                                }
                                s_min_length += 1;
                            }
                        }
                        else {
                            s_min = new String[59 - Integer.parseInt(start_time_min_2.getText().toString())];
                            int s_min_length = 0;
                            for (int i = Integer.parseInt(start_time_min_2.getText().toString()) + 1; i < 60; i++) {
                                if (String.valueOf(i).length() == 1) {
                                    s_min[s_min_length] = "0" + i;
                                }
                                else {
                                    s_min[s_min_length] = String.valueOf(i);
                                }
                                s_min_length += 1;
                            }
                        }
                    }
                    else {
                        if (Integer.parseInt(start_time_min.getText().toString()) > 0) {
                            if (Integer.parseInt(start_time_hour.getText().toString()) < Integer.parseInt(start_time_hour_2.getText().toString())) {
                                s = new String[24 - Integer.parseInt(start_time_hour_2.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString())];
                            }
                            else if (Integer.parseInt(start_time_hour.getText().toString()) == Integer.parseInt(start_time_hour_2.getText().toString()) && Integer.parseInt(start_time_min.getText().toString()) < Integer.parseInt(start_time_min_2.getText().toString())) {
                                s = new String[1];
                            }
                            else {
                                s = new String[Integer.parseInt(start_time_hour.getText().toString()) - Integer.parseInt(start_time_hour_2.getText().toString())];
                            }
                        }
                        else {
                            if (Integer.parseInt(start_time_hour.getText().toString()) < Integer.parseInt(start_time_hour_2.getText().toString())) {
                                s = new String[24 - Integer.parseInt(start_time_hour_2.getText().toString()) + Integer.parseInt(start_time_hour.getText().toString()) - 1];
                            }
                            else if (Integer.parseInt(start_time_hour.getText().toString()) == Integer.parseInt(start_time_hour_2.getText().toString()) && Integer.parseInt(start_time_min.getText().toString()) < Integer.parseInt(start_time_min_2.getText().toString())) {
                                s = new String[1];
                            }
                            else {
                                s = new String[Integer.parseInt(start_time_hour.getText().toString()) - Integer.parseInt(start_time_hour_2.getText().toString()) - 1];
                            }
                        }
                        s_min = new String[0];
                    }
                    if (Integer.parseInt(start_time_hour.getText().toString()) < Integer.parseInt(start_time_hour_2.getText().toString())) {
                        for (int i = Integer.parseInt(start_time_hour_2.getText().toString()) + 1; i < 24; i++) {
                            if (i < 10) {
                                s[s_length] = "0" + i;
                            }
                            else {
                                s[s_length] = String.valueOf(i);
                            }
                            s_length += 1;
                        }
                        for (int i = 0; i < Integer.parseInt(start_time_hour.getText().toString()); i++) {
                            if (i < 10) {
                                s[s_length] = "0" + i;
                            }
                            else {
                                s[s_length] = String.valueOf(i);
                            }
                            s_length += 1;
                        }
                    }
                    else {
                        for (int i = Integer.parseInt(start_time_hour_2.getText().toString()) + 1; i < Integer.parseInt(start_time_hour.getText().toString()); i++) {
                            if (i < 10) {
                                s[s_length] = "0" + i;
                            }
                            else {
                                s[s_length] = String.valueOf(i);
                            }
                            s_length += 1;
                        }
                    }

                    if (Integer.parseInt(start_time_min.getText().toString()) > 0 && Integer.parseInt(start_time_hour.getText().toString()) != Integer.parseInt(start_time_hour_2.getText().toString())) {
                        s[s_length] = start_time_hour.getText().toString();
                        last_min = new String[Integer.parseInt(start_time_min.getText().toString())];
                        int last_min_length = 0;
                        for (int i = 0; i < Integer.parseInt(start_time_min.getText().toString()); i++) {
                            if (i < 10) {
                                last_min[last_min_length] = "0" + i;
                            }
                            else {
                                last_min[last_min_length] = String.valueOf(i);
                            }
                            last_min_length += 1;
                        }
                    }
                    else {
                        last_min = new String[0];
                    }

                    if (s.length > 1 || s_min.length > 1) {
                        Dialog dialog = new Dialog(view.getContext());
                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                        dialog.setContentView(R.layout.choose_end_time_bottomsheet);
                        NumberPicker min = dialog.findViewById(R.id.min);
                        NumberPicker hour = dialog.findViewById(R.id.hour);
                        hour.setDisplayedValues(s);
                        hour.setMaxValue(s.length - 1);
                        hour.setMinValue(0);
                        hour.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                        hour.setValue(0);
                        hour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                            @Override
                            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                                System.out.println(numberPicker.getValue());
                                System.out.println(s.length);

                                if (numberPicker.getValue() == 0 && s_min.length > 0) {
                                    min.setDisplayedValues(null);
                                    min.setMaxValue(s_min.length - 1);
                                    min.setDisplayedValues(s_min);
                                    min.setMinValue(0);
                                    min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                    min.setValue(0);
                                }
                                else if (numberPicker.getValue() == s.length - 1 && last_min.length > 0) {
                                    min.setDisplayedValues(null);
                                    min.setMaxValue(last_min.length - 1);
                                    min.setDisplayedValues(last_min);
                                    min.setMinValue(0);
                                    min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                    min.setValue(0);
                                }
                                else {
                                    min.setDisplayedValues(null);
                                    min.setMaxValue(all_min.length - 1);
                                    min.setDisplayedValues(all_min);
                                    min.setMinValue(0);
                                    min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                                    min.setValue(0);
                                }
                            }
                        });

                        if (hour.getValue() == 0 && s_min.length > 0) {
                            min.setMaxValue(s_min.length - 1);
                            min.setDisplayedValues(s_min);
                            min.setMinValue(0);
                            min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                            min.setValue(0);
                        }
                        else if (hour.getValue() == s.length - 1 && last_min.length > 0) {
                            min.setMaxValue(last_min.length - 1);
                            min.setDisplayedValues(last_min);
                            min.setMinValue(0);
                            min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                            min.setValue(0);
                        }
                        else {
                            min.setMaxValue(all_min.length - 1);
                            min.setDisplayedValues(all_min);
                            min.setMinValue(0);
                            min.setDescendantFocusability(DatePicker.FOCUS_BEFORE_DESCENDANTS);
                            min.setValue(0);
                        }
                        TextView finish = dialog.findViewById(R.id.finish);
                        finish.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                end_time_hour_2.setText(s[hour.getValue()]);
                                if (hour.getValue() == 0 && s_min.length > 0) {
                                    end_time_min_2.setText(s_min[min.getValue()]);
                                }
                                else if (hour.getValue() == s.length - 1 && last_min.length > 0) {
                                    end_time_min_2.setText(last_min[min.getValue()]);
                                }
                                else {
                                    end_time_min_2.setText(all_min[min.getValue()]);
                                }
                                dialog.dismiss();
                            }
                        });
                        TextView cancel = dialog.findViewById(R.id.cancel);
                        cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                dialog.dismiss();
                            }
                        });
                        dialog.show();
                        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                        dialog.getWindow().setGravity(Gravity.BOTTOM);
                    }
                    else {
                        Toast.makeText(Set_Open_Shop_Time.this, "没有可选择的时间", Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(Set_Open_Shop_Time.this, "请先选择起始时间", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    void set_open_day_text() {
        open_day_text = "";
        is_mon = false;
        is_tue = false;
        is_wed = false;
        is_thu = false;
        is_fri = false;
        is_sat = false;
        is_sun = false;
        for (int i = 0; i < open_day.length(); i += 2) {
            if (open_day.charAt(i) == '1') {
                is_mon = true;
            }
            else if (open_day.charAt(i) == '2') {
                is_tue = true;
            }
            else if (open_day.charAt(i) == '3') {
                is_wed = true;
            }
            else if (open_day.charAt(i) == '4') {
                is_thu = true;
            }
            else if (open_day.charAt(i) == '5') {
                is_fri = true;
            }
            else if (open_day.charAt(i) == '6') {
                is_sat = true;
            }
            else if (open_day.charAt(i) == '7') {
                is_sun = true;
            }
        }
        Character start_day = open_day.charAt(0);
        Character end_day = open_day.charAt(0);
        open_day_text += week.get(open_day.charAt(0));
        System.out.println(open_day.charAt(0));
        System.out.println(open_day_text);
        for (int i = 2; i < open_day.length(); i += 2) {
            if (open_day.charAt(i) - end_day == 1) {
                end_day = open_day.charAt(i);
            }
            else {
                if (!start_day.equals(end_day)) {
                    open_day_text += "-";
                    open_day_text += week.get(end_day);
                }
                open_day_text += ",";
                open_day_text += week.get(open_day.charAt(i));
                end_day = open_day.charAt(i);
                start_day = open_day.charAt(i);
            }
        }
        if (!start_day.equals(end_day)) {
            open_day_text += "-";
            open_day_text += week.get(end_day);
        }
        System.out.println(open_day_text);

        if (open_day_text.equals("周一-周日")) {
            which_day_open.setText("每天");
        }
        else {
            which_day_open.setText(open_day_text);
        }
    }
}